package com.dl.tt.control;

import java.util.List;
import java.util.Stack;
import java.util.Vector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dl.tt.bean.Stuff;
import com.dl.tt.bean.User;
import com.dl.tt.service.StuffService;
import com.dl.tt.service.UserService;

@Controller
public class UserLogin {

	@Autowired 
	private StuffService stuffService;
	
	public  UserLogin() {
		System.out.println("--------------UserLogin--------------------");
	}
	@Autowired
	private UserService userService;
	
	@RequestMapping("userlogin")
	public String userlogin(User user,Model model) {
		System.out.println("userlogin->"+user);
		boolean result = this.userService.findUserByNameAndPwd(user.getName(), user.getPassword());
		
		if(result) {
			
			List<Stuff> stufflist=this.stuffService.findAll();
			model.addAttribute("slist", stufflist);
			return "stuffdisplay";
		}
		
		return "error";
	}
	
	// del?id=1001
	@RequestMapping("del")
	public String del(int id,Model model) {
		
		this.stuffService.del(id);
		
		// 删除完成之后，需要做那些事情，重新查询数据，然后放到model中
		model.addAttribute("slist", this.stuffService.findAll());
		
		return "stuffdisplay";
	}
	
	
	@RequestMapping("insert")
	public String insert() {
		return "insert";
	}
	
	@RequestMapping("save")
	public String save(Stuff stuff,Model model) {
		// 插入数据，调用service
		this.stuffService.insert(stuff);
		// 重新获取数据
		List<Stuff> list = this.stuffService.findAll();
		model.addAttribute("slist", list);
		
		return "stuffdisplay";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
